set -e
if [ ! -d ".git" ]; then
    git init
    git add .
    git commit -m "first commit"
    git remote add origin git@gitee.com:linuxwhata/mirrors.git
fi

urls=(
    "https://github.com/mic92/sops-nix"
    "https://github.com/nix-community/disko"
    "https://github.com/nix-community/impermanence"
    "https://github.com/nix-community/plasma-manager"
    "https://github.com/nix-community/nixos-vscode-server"
)

for url in ${urls[@]}
do
    if ! git remote | grep -q "^${url##*/}\$"; then
        git remote add ${url##*/} $url
    fi
    if [ -d "${url##*/}" ]; then
        echo "跳过 ${url##*/}"
        continue
    fi
    branch=$(LC_ALL=C git remote show ${url##*/} | awk '/HEAD branch/ {print $NF}')
    echo "添加 $branch: $url"
    git subtree add --prefix=${url##*/} $url $branch --squash
done
